package api

import (
	"github.com/gin-gonic/gin"
	"github.com/qiniu/api.v7/auth/qbox"
	"github.com/qiniu/api.v7/storage"
	"fmt"
	"net/http"
	"constant"
)

const accessKey = "eK2CZvPB76HtVntREhwEl5_BYsVHqVcxkvf0A3k3"
const secretKey = "QKzS4O4da8TiB_FxB8SfplBobuqI148Q_Ie-YV0M"
const bucket = "devtest"

// 获取七牛云token
func GetUploadToken(ctx *gin.Context) {

	fileName := ctx.Query("fileName")

	putPolicy := storage.PutPolicy{
		Scope:      fmt.Sprintf("%s:%s", bucket, fileName),
		ReturnBody: `{"key":"$(key)","hash":"$(etag)","fsize":$(fsize),"bucket":"$(bucket)","name":"$(x:name)"}`,
	}

	mac := qbox.NewMac(accessKey, secretKey)

	upToken := putPolicy.UploadToken(mac)

	result := constant.SUCCESS
	result.Info = gin.H{
		"fileName": fileName,
		"upToken":  upToken,
	}

	ctx.JSON(http.StatusOK, result)
}
